package controller

import (
	"Family/app/dot"
	"Family/app/entity"
	"Family/app/model"
	"strconv"

	"github.com/gin-gonic/gin"
)

var db model.MemberModel

func AddMember(ctx *gin.Context) {
	var param dot.AddMemberReq
	err := ctx.ShouldBind(&param)
	if err != nil {
		Failed(ctx, "参数解析失败", GetLine())
		return
	}

	Gender, _ := strconv.Atoi(param.Gender)
	FatherId, _ := strconv.Atoi(param.FatherId)
	MotherId, _ := strconv.Atoi(param.MotherId)
	var models entity.Member
	models.Nickname = param.Nickname
	models.Gender = Gender
	models.Birthdate = param.Birthdate
	models.FatherId = FatherId
	models.MotherId = MotherId
	models.Intro = param.Intro
	models.CreateTime = ThisTime()
	result := db.AddMember(&models)
	if result == false {
		Failed(ctx, "系统异常，添加成员失败", GetLine())
		return
	}

	Success(ctx, nil, "添加成功")
}

func UpdateMember(ctx *gin.Context) {
	var param dot.UpdateMemberReq
	err := ctx.ShouldBind(&param)
	if err != nil {
		Failed(ctx, "参数解析失败", GetLine())
		return
	}

	memberInfo := db.FindMemberById(param.MemberId)
	if memberInfo.Id == 0 {
		Failed(ctx, "修改失败，成员不存在", GetLine())
		return
	}

	updateData := map[string]interface{}{
		"nickname":  param.Nickname,
		"gender":    param.Gender,
		"birthdate": param.Birthdate,
		"intro":     param.Intro,
	}

	result := db.UpdateMemberById(param.MemberId, updateData)
	if result == false {
		Failed(ctx, "修改失败，服务器出现异常", GetLine())
		return
	}
	Success(ctx, nil, "修改成功")
}

func GetList(ctx *gin.Context) {
	page := ctx.Query("page")
	limit := ctx.Query("limit")

	pages, _ := strconv.Atoi(page)
	limits, _ := strconv.Atoi(limit)

	param := dot.GetListReq{
		Page:  pages,
		Limit: limits,
	}
	list, count := db.GetList(param)

	var res []dot.GetListResp
	if count > 0 {
		for _, value := range list {
			var item dot.GetListResp
			item.Id = value.Id
			item.CreateTime = value.CreateTime
			item.Nickname = value.Nickname
			item.Gender = value.Gender
			item.Birthdate = value.Birthdate
			item.Deathdate = value.Deathdate
			item.Marriagedate = value.Marriagedate
			item.Intro = value.Intro
			item.MateId = value.MateId
			item.FatherId = value.FatherId
			item.MotherId = value.MotherId

			FatherInfo := db.FindMemberById(strconv.Itoa(value.FatherId))
			if FatherInfo.Id != 0 {
				item.FatherName = FatherInfo.Nickname
			}

			MateInfo := db.FindMemberById(strconv.Itoa(value.MateId))
			if MateInfo.Id != 0 {
				item.MateName = MateInfo.Nickname
			}

			MotherInfo := db.FindMemberById(strconv.Itoa(value.MotherId))
			if MotherInfo.Id != 0 {
				item.MotherName = MotherInfo.Nickname
			}
			res = append(res, item)
		}
	}
	result := List{
		List:  res,
		Count: int(count),
	}

	Success(ctx, result, "success")
}

func DelMember(ctx *gin.Context) {
	var param dot.DeleteReq
	err := ctx.ShouldBind(&param)
	if err != nil {
		Failed(ctx, "参数解析失败", GetLine())
		return
	}

	memberInfo := db.FindMemberById(strconv.Itoa(param.MemberId))
	if memberInfo.Id == 0 {
		Failed(ctx, "修改失败，成员不存在", GetLine())
		return
	}

	result := db.Delete(strconv.Itoa(param.MemberId))
	if result == false {
		Failed(ctx, "删除失败，服务器出现异常", GetLine())
		return
	}

	Success(ctx, nil, "删除成功")
}

func GetMemberList(ctx *gin.Context) {
	var param dot.GetListReq
	param.Page = 1
	param.Limit = 10000
	memberList, _ := db.GetList(param)
	var fatherList, motherList []entity.Member
	for _, value := range memberList {
		if value.Gender == 1 {
			if value.MateId != 0 {
				fatherList = append(fatherList, value)
			}
		} else {
			if value.MateId != 0 {
				motherList = append(motherList, value)
			}
		}
	}
	result := make(map[string][]entity.Member)
	result["fatherList"] = fatherList
	result["motherList"] = motherList
	Success(ctx, result, "success")
}

func GetMateList(ctx *gin.Context) {
	var param dot.GetListReq
	param.Page = 1
	param.Limit = 10000
	memberList, _ := db.GetList(param)
	var man, women []entity.Member
	for _, value := range memberList {
		if value.Gender == 1 {
			if value.MateId == 0 {
				man = append(man, value)
			}
		} else {
			if value.MateId == 0 {
				women = append(women, value)
			}
		}
	}

	result := make(map[string][]entity.Member)
	result["man"] = man
	result["women"] = women
	Success(ctx, result, "success")
}

func SetMate(ctx *gin.Context) {
	var param dot.SetMateReq
	err := ctx.ShouldBind(&param)
	if err != nil {
		Failed(ctx, "参数解析失败", GetLine())
		return
	}

	memberInfo := db.FindMemberById(param.UserId)
	if memberInfo.Id == 0 {
		Failed(ctx, "修改失败，成员不存在", GetLine())
		return
	}

	mateTime := ThisTime()

	updateData := map[string]interface{}{
		"mate_id":      param.MateId,
		"marriagedate": mateTime,
	}

	result := db.UpdateMemberById(param.UserId, updateData)
	if result == false {
		Failed(ctx, "修改失败，服务器出现异常", GetLine())
		return
	}

	updateData = map[string]interface{}{
		"mate_id":      param.UserId,
		"marriagedate": mateTime,
	}
	result = db.UpdateMemberById(param.MateId, updateData)
	if result == false {
		Failed(ctx, "修改失败，服务器出现异常", GetLine())
		return
	}

	Success(ctx, nil, "修改成功")
}
